package com.meessiom.train.crm.employee.service;
import com.meessiom.train.crm.common.exception.ResourceNotFoundException;
import com.meessiom.train.crm.employee.model.Employee;
import com.meessiom.train.crm.employee.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class EmployeeService {

   // public Employee updateEmployeeid;
    @Autowired
    private EmployeeRepository employeeRepository;
    public List<Employee> getAllEmployees(){
        return employeeRepository.findAll();
    }


//    public Employee createEmployee(Employee employee){
//        return employeeRepository.save(employee);
//
//    }


    public List<Employee> createEmployees(List<Employee> employees){
        return employeeRepository.saveAll(employees);

    }

    public Employee getEmployeeById(long id) {
        Employee employee = employeeRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Employee not exist with id" + id));
        return employee;
    }

    public String getEmployeeNameByEmployeeId(long id){
        String employeeName = employeeRepository.getEmployeeNameByEmployeeId(id);
        return employeeName;
    }

    public Employee updataEmployee(long id,Employee employeeDetails) {
        Employee updateEmployee = employeeRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Employee not exist with id" + id));
        updateEmployee.setEmployeeName(employeeDetails.getEmployeeName());
        updateEmployee.setEmployeePhone(employeeDetails.getEmployeePhone());
        updateEmployee.setEmployeeAddress(employeeDetails.getEmployeeAddress());
        updateEmployee.setEmployeeGender(employeeDetails.getEmployeeGender());
        updateEmployee.setEmployeeJob(employeeDetails.getEmployeeJob());

        employeeRepository.save(updateEmployee);
        return updateEmployee;
    }
        public void deleteEmployee(long id) {
            Employee employee = employeeRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Employee not exist with id" + id));

            employeeRepository.delete(employee);
        }

}


